#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;

class tour
{
public:
    int size=0;
    vector<string>member;
    int capcacity=0;
public:
    tour(int _capacity):capcacity(_capacity){}
    ~tour()=default;
};
typedef tour tour_team;
void Print(unordered_map<char,tour_team*>& mp,char key)
{
    cout<<mp[key]->size<<" ";
    for(auto& mem:mp[key]->member)
        cout<<mem<<" ";
    cout<<endl;
}
void Delete(unordered_map<char, tour_team*>& mp)
{
    for(auto& [a,b]:mp)
    {
        delete b;
        b=nullptr;
    }
}
int main() {
    unordered_map<char, tour_team*> tt_mp={};
    int N=0;
    int a=0,b=0,c=0;
    cin>>N;
    cin>>a>>b>>c;
    tour_team* A=new tour_team(a);
    tour_team* B=new tour_team(b);
    tour_team* C=new tour_team(c);
    tt_mp.insert({'A',A});
    tt_mp.insert({'B',B});
    tt_mp.insert({'C',C});
    string name;
    int num;
    vector<char>like;
    char like_member;
    while(N--)
    {
        name="";
        num=0;
        like.clear();
        cin>>name;
        cin>>num;
        while(num--)
        {
            cin>>like_member;
            like.push_back(like_member);
            // cout<<like_member<<" ";
            like_member=' ';
        }
        for(auto key:like)
        {
            if(tt_mp[key]->size>=tt_mp[key]->capcacity)
                continue;
            else
            {
                tt_mp[key]->size++;
                tt_mp[key]->member.push_back(name);
                break;
            }
        }
    }

    Print(tt_mp,'A');
    Print(tt_mp,'B');
    Print(tt_mp,'C');
    for(auto& [a,b]:tt_mp)
        Delete(tt_mp);
    // delete tt_mp['A'];
    // tt_mp['A']=nullptr;
    // delete tt_mp['B'];
    // tt_mp['B']=nullptr;
    // delete tt_mp['C'];
    // tt_mp['C']=nullptr;
    tt_mp.clear();
    return 0;
}
// 64 位输出请用 printf("%lld")